/**
 * 作者: wgx
 * 日期: 2025-04-18 09:34:55
 * 备注: 用户act_task表的奖励领取记录
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserActTaskReward;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserActTaskRewardModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id.task_group_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // id
    protected long userId; // 用户Id
    protected int taskGroupId; // 任务的groupid
    protected String rewardInfo = ""; // 奖励领取记录

    public void reset() {
    }

    protected String getKey() {
        return "UserActTaskReward." + userId + "." + taskGroupId;
    }

    protected String getListKey() {
        return "UserActTaskReward." + userId;
    }

    protected String getListItemKey() {
        return taskGroupId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_act_task_reward"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // id
        obj.put("userId", userId); // 用户Id
        obj.put("taskGroupId", taskGroupId); // 任务的groupid
        obj.put("rewardInfo", rewardInfo); // 奖励领取记录
        return obj;
    }

    public PBUserActTaskReward.Builder takeInitBuilder() {
        PBUserActTaskReward.Builder sb = PBUserActTaskReward.newBuilder();
        if (taskGroupId != 0) {
            sb.setTaskGroupId(taskGroupId);
        }
        if (this.rewardInfo != null && this.rewardInfo.length() > 0) {
            JsonArray list = new JsonArray(this.rewardInfo);
            for(int i=0; i<list.size(); i++) {
                sb.addRewardInfo(list.getInteger(i));
            }
        }
        return sb;
    }

    public PBUserActTaskReward.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserActTaskReward.Builder sb = PBUserActTaskReward.newBuilder();
        if (isFieldUpdate("taskGroupId")) {
            sb.setTaskGroupId(taskGroupId);
        }
        if (isFieldUpdate("rewardInfo")) {
            if (this.rewardInfo != null && this.rewardInfo.length() > 0) {
                JsonArray list = new JsonArray(this.rewardInfo);
                for(int i=0; i<list.size(); i++) {
                    sb.addRewardInfo(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearRewardInfo();
                }
            } else {
                sb.clearRewardInfo();
            }
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public int getTaskGroupId() {
        return taskGroupId;
    }

    public void putTaskGroupId(int taskGroupId) {
        if (this.taskGroupId != taskGroupId) {
            this.taskGroupId = taskGroupId;
            updateRedisHashField(getKey(), "taskGroupId", this.taskGroupId);
        }
    }

    public void addTaskGroupId(int value) {
        this.taskGroupId += value;
        updateRedisHashField(getKey(), "taskGroupId", this.taskGroupId);
    }

    public JsonArray getRewardInfoArray() {
        if (this.rewardInfo != null && this.rewardInfo.length() > 0) {
            return new JsonArray(this.rewardInfo);
        } else {
            return new JsonArray();
        }
    }

    public String getRewardInfo() {
        return rewardInfo;
    }

    public void putRewardInfo(String rewardInfo) {
        if (!this.rewardInfo.equals(rewardInfo)) {
            this.rewardInfo = rewardInfo;
            updateRedisHashField(getKey(), "rewardInfo", this.rewardInfo);
        }
    }

}